package com.ruoyi.bantong.controller;

//import com.ruoyi.bantong.domain.BtUser;
import com.ruoyi.bantong.service.IBtUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.BtUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户Controller
 * 
 * @author laohei
 * @date 2021-01-24
 */
@RestController
@RequestMapping("/bantong/user")
public class BtUserController extends BaseController
{
    @Autowired
    private IBtUserService btUserService;

    /**
     * 查询用户列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(BtUser btUser)
    {
        startPage();
        List<BtUser> list = btUserService.selectBtUserList(btUser);
        return getDataTable(list);
    }


    /**
     * 查询用户列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:list')")
    @GetMapping("/friendList")
    public TableDataInfo friendList(BtUser btUser)
    {
        btUser.setUserId(SecurityUtils.getBtUserId());
        startPage();
        List<BtUser> list = btUserService.selectBtUserListByInvitationCode(btUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BtUser btUser)
    {
        List<BtUser> list = btUserService.selectBtUserList(btUser);
        ExcelUtil<BtUser> util = new ExcelUtil<BtUser>(BtUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取用户详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Integer userId)
    {
        return AjaxResult.success(btUserService.selectBtUserById(userId));
    }

    /**
     * 获取用户被充值金豆
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:query')")
    @GetMapping(value = "/getUserRechargeSum/{userId}")
    public AjaxResult getUserRechargeSum(@PathVariable("userId") Integer userId)
    {
        return AjaxResult.success(btUserService.selectBtBeanRechargeSumById(userId));
    }

    /**
     * 新增用户
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BtUser btUser)
    {
        return toAjax(btUserService.insertBtUser(btUser));
    }

    /**
     * 修改用户  前端提交用户使用！
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(BtUser btUser)
    {
        if(SecurityUtils.getBtUserId() != null && SecurityUtils.getBtUserId() != 0){
            btUser.setUserId(SecurityUtils.getBtUserId());
        }
        String userCard = btUser.getUserCard();
        String idcardNum = btUser.getIdcardNum();
        String invitationCode = btUser.getToInvitationCode();
        if(!StringUtils.isEmpty(invitationCode)){
            BtUser btUser1 = btUserService.selectBtUserByInvitationCode(invitationCode, SecurityUtils.getBtUserId());
            if(StringUtils.isNull(btUser1)) {
                return AjaxResult.error(invitationCode + "' 邀请码不存在");
            }
            //判断当前用户是否有 填写过邀请码
            BtUser btUser2 = btUserService.selectBtUserById(SecurityUtils.getBtUserId());
            String toInvitationCode = btUser2.getToInvitationCode();
            if(!StringUtils.isEmpty(toInvitationCode)){
                return AjaxResult.error("请勿重复绑定邀请码");
            }
        }
//        BtUser btUserTemp = btUserService.selectBtUserByCard(userCard,SecurityUtils.getBtUserId());
//        if(!StringUtils.isNull(btUserTemp)){
//            return AjaxResult.error("修改用户失败，'" + userCard + "'卡号已存在！");
//        }
        BtUser btUserTemp = btUserService.selectBtUserByIdcardNum(idcardNum);
        if(!StringUtils.isNull(btUserTemp)){
            return AjaxResult.error("修改用户失败，'" + idcardNum + "'该身份证号已存在！");
        }
        return toAjax(btUserService.updateBtUser(btUser));
    }


    /**
     * 修改用户 PC端使用
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody BtUser btUser)
    {
        return toAjax(btUserService.updateBtUser(btUser));
    }

    /**
     * 充值
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("/recharge")
    public AjaxResult recharge(@RequestBody BtUser btUser)
    {
        int rows = 0;
        try {
            rows = btUserService.recharge(btUser);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof CustomException){
                return AjaxResult.error(e.getMessage());
            }else {
                return AjaxResult.error();
            }
        }
    }

    /**
     * 充值 积分
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("/rechargeScore")
    public AjaxResult rechargeScore(@RequestBody BtUser btUser)
    {
        return toAjax(btUserService.rechargeScore(btUser));
    }

    /**
     * 充值 奖券
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("/rechargeTicket")
    public AjaxResult rechargeTicket(@RequestBody BtUser btUser)
    {
        return toAjax(btUserService.rechargeTicket(btUser));
    }

    /**
     * 审核用户
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public AjaxResult check(@RequestBody BtUser btUser)
    {
        return toAjax(btUserService.checkBtUser(btUser));
    }

    /**
     * 删除用户
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Integer[] userIds)
    {
        return toAjax(btUserService.deleteBtUserByIds(userIds));
    }
}
